//
//  VCPhotoViewController.m
//  NBTripe
//
//  Created by 陈曦 on 16/1/16.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "VCPhotoViewController.h"
#import "photoModel.h"
#import "VCPhotoCollectionViewCell.h"
#import "VCPhotoNextViewController.h"

@interface VCPhotoViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,strong) NSMutableArray *array;
@property (nonatomic,strong) NSNumber *start;

@end

@implementation VCPhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.start = [NSNumber numberWithInt:18];
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    // item的最小行间距
    _flowLayout.minimumLineSpacing = 10;
    // item的最小列间距
    _flowLayout.minimumInteritemSpacing = 5;
    // 滑动方向
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(10,  0, self.view.width - 20, self.view.height - 110 ) collectionViewLayout:self.flowLayout];
    // item的大小
    self.flowLayout.itemSize = CGSizeMake(self.collectionView.width / 3 - 5, self.collectionView.width / 3 - 5);
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor colorWithRed:250/255.0 green:246/255.0 blue:232/255.0 alpha:1];
    [self.collectionView registerClass:[VCPhotoCollectionViewCell class] forCellWithReuseIdentifier:@"VCPhotoCollectionViewCell"];
    [self.view addSubview:self.collectionView];
    [self getDataSource];

    
    
    // Do any additional setup after loading the view.
}
- (void)getDataSource {
    NSString *url = [NSString stringWithFormat:@"%@/photos/?gallery_mode=1&count=18&sign=4ecb95d2e66cc50898a0e9cf7cd7d267", self.str];
    [XYNetWorkTool getWithUrl:url parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        NSArray *arr = result[@"items"];
        NSMutableArray *mutableArr = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in arr) {
            photoModel *model = [[photoModel alloc] initWithDataSource:dic];
            [mutableArr addObject:model];
        }
        self.array = [NSMutableArray arrayWithArray:mutableArr];
        [self.collectionView reloadData];
        
    } fail:^(NSError *error) {
        NSLog(@"目的地的首页数据获取失败，错误为%@", error);
    }];
}
- (void)reGetDataSource {
    NSString *url = [NSString stringWithFormat:@"%@/photos/?gallery_mode=1&count=18&start=%@&sign=4ecb95d2e66cc50898a0e9cf7cd7d267", self.str,self.start];
    [XYNetWorkTool getWithUrl:url parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        NSArray *arr = result[@"items"];
        NSMutableArray *mutableArr = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in arr) {
            photoModel *model = [[photoModel alloc] initWithDataSource:dic];
            [mutableArr addObject:model];
        }
        [self.array addObjectsFromArray:mutableArr];
        self.start = [NSNumber numberWithInt:(self.start.intValue + 18)];
        [self.collectionView reloadData];

        
    } fail:^(NSError *error) {
        NSLog(@"目的地的首页数据获取失败，错误为%@", error);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    VCPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"VCPhotoCollectionViewCell" forIndexPath:indexPath];
    cell.name = [self.array[indexPath.row] photo_s];
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    VCPhotoNextViewController *photoNextVC = [[VCPhotoNextViewController alloc] init];
    photoNextVC.arr = self.array;
    photoNextVC.index = indexPath.row;
    [self.navigationController pushViewController:photoNextVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
